#include "xml_device_object.h"
#include "v2/control/cairx.h"
#include "v2/control/cwindowx.h"
#include "v2/control/cscenex.h"
#include "v2/control/cdimmingx.h"
#include "v2/control/cairx.h"
#include "v2/control/clabelx.h"
#include "v2/control/cmultimediax.h"
#include "v2/control/cswitchx.h"
#include "v2/control/cremote.h"
#include "knx/cmei.h"

Xml_DEVICE_OBJECT::Xml_DEVICE_OBJECT(QDomElement obj, void *cntDelegate)
{
    _regList = NULL;
    _usrcontrol = NULL;
    _read_times = 0;

    {
        QString device_name = obj.tagName();

        _id = obj.attributeNode("id").value();
        _zone = obj.attributeNode("zone").value();
        _type = obj.attributeNode("type").value();
        //以前用来做KNX的type，现在基本为空值
        _type = device_name;
        _name = obj.attributeNode("name").value();
        _exp = obj.attributeNode("exp").value();
        _status = obj.attributeNode("status").value();
        _send = obj.attributeNode("send").value();
        _dim_send = obj.attributeNode("dim_value").value();
        _stop_send = obj.attributeNode("stop").value();
        _mode_send = obj.attributeNode("mode").value();
        _wind_send = obj.attributeNode("wind").value();
        _selection = obj.attributeNode("selection").value();

        if(device_name == "object")
        {
            _usrcontrol = new CSwitchx();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(device_name == "scene")
        {
            _usrcontrol = new CScenex();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(device_name == "dim")
        {
            _usrcontrol = new CDimmingx();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(device_name == "window")
        {
            _usrcontrol = new CWindowx();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(device_name == "air")
        {
            _usrcontrol = new CAirx();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->parseSelection(_selection);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(device_name == "label")
        {
            _usrcontrol = new CLabelx();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(device_name == "music-remote")
        {
            _usrcontrol = new Cmultimediax();
            _usrcontrol->cntDelegate = cntDelegate;
            _usrcontrol->setKnxObject(this);
        }
        else if(device_name == "tv-remote")
        {
            _usrcontrol = new CRemote();
            _usrcontrol->cntDelegate = cntDelegate;
            _usrcontrol->setKnxObject(this);
        }


        knxRegListRequest();
    }
}

Xml_DEVICE_OBJECT::~Xml_DEVICE_OBJECT()
{
    removeDevList();
}

void Xml_DEVICE_OBJECT::doAction(QString action,QString value)
{
    if(_usrcontrol)
        _usrcontrol->execKnxAction(action,value);
}

void Xml_DEVICE_OBJECT::doRead()
{
    if(_usrcontrol)
    {
        _usrcontrol->readKnxAction();
        _read_times ++;
    }
}

void Xml_DEVICE_OBJECT::removeDevList()
{
    if(_regList!=NULL)
    {
        for(int i=0;i<_regList->size();i++)
        {
            delete _regList->at(i);
        }

        _regList->clear();
        _regList = NULL;
    }
}

QList<DEVICE_OBJECT *> * Xml_DEVICE_OBJECT::knxRegListRequest()
{
    removeDevList();

    if(_status.isEmpty())
        return NULL;

    _regList = new QList<DEVICE_OBJECT *>();

    QStringList feedbackList = _status.split(",");
    for(int i=0;i<feedbackList.size();i++)
    {
        QStringList fl = feedbackList.at(i).split(":");
	//knx address should not be 0 or null
        if(fl.size()>=3 && fl.at(0).length()>3 && fl.at(0).contains("/"))
        {
            DEVICE_OBJECT *d = new DEVICE_OBJECT();

            d->_name = _id;
            d->_address = fl.at(0).toUtf8();
            d->_data_type = (DATA_TYPE)fl.at(1).toUInt();
            d->_data_define = (DATA_DEFINE)fl.at(2).toUInt();
            d->_control = _type;

            if(d->_address.isEmpty())
                continue;

            cmei_set_group_address(d->_ga_ia,d->_address);

            _regList->append(d);
        }
    }

    return _regList;
}

QString Xml_DEVICE_OBJECT::actionFromStatus(QString address,QString value)
{
    if(_usrcontrol)
    {
        return _usrcontrol->actionFromStatus(address,value);
    }

    return "";
}

QString Xml_DEVICE_OBJECT::valueCheck(QString action,QString value)
{
    if(_usrcontrol)
    {
        return _usrcontrol->valueCheck(action,value);
    }

    return value;
}
